package dyyx.demo;

import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.GroupFactory;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.MessageTypeParser;

import dyyx.util.CommUtil;

public class WriterTest {

	public static void main(String[] args) throws Exception {
		System.out.println("hello,WriterTest," + new Date());

		System.out.println(CommUtil.getUserHome());

		String file = CommUtil.getUserHome() + "/tmp/parquet.data";

		System.out.println("file=" + file);

		// INT64, INT32, BOOLEAN, BINARY, FLOAT, DOUBLE, INT96,
		// FIXED_LEN_BYTE_ARRAY
		String schema = "message man {required int32 id;required BINARY name(UTF8);}";
		MessageType messageType = MessageTypeParser.parseMessageType(schema);

		System.out.println("schema=" + schema);
		System.out.println("messageType=" + messageType);

		GroupFactory factory = new SimpleGroupFactory(messageType);
		Path path = new Path(file);
		GroupWriteSupport writeSupport = new GroupWriteSupport();
		Configuration configuration = new Configuration();
		writeSupport.setSchema(messageType, configuration);

		// org.apache.hadoop.fs.FileAlreadyExistsException: File already exists

		ParquetWriter<Group> writer = new ParquetWriter<Group>(path, configuration, writeSupport);

		// writeSupport.prepareForWrite(recordConsumer);

		int num = 100;
		for (int i = 0; i < num; i++) {
			Group group = factory.newGroup().append("id", i).append("name",
					"man_" + i + "_" + System.currentTimeMillis());

			writer.write(group);
		}

		writer.close();

		System.out.println("Writer done," + new Date() + ",num=" + num);

	}
}
